<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
include("../includes/file.php");
require('./configs/site_config.php');
class Upload_File {
	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/

	public function __construct(){
		
	}
	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index() {
	

		if (empty($GLOBALS['web_upload'][$_GET['web_type']]['path'])) {
			
			echo '类型找不到目录！';
			exit;
		}
		
		$basepath = DOC_ROOT.$GLOBALS['web_upload'][$_GET['web_type']]['path'];//定义目录
		$allowType = 'jpg,jpeg,png,gif,txt,doc,zip,rar,css,htm,html,js,bmp,swf,as,php,xml,pdf';
		$allowMime = '';
		
		chdir($basepath);
		
		$path = isset($_REQUEST['path']) ? $_REQUEST['path'] : '';
		if ( $path ) {
			$path = ereg_replace("^[\/\\\.]+","", $path);
		}
		$path = './' . $path;
		
		$path = iconv('utf-8','gb2312', ereg_replace("[\/\\]+", "/", $path));
		
		if ( !is_dir($path) ) {
			
			alert("目录错误", 'location',$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
		}
		
		$data = array(	'dir' => $path,
						'allowType' => $allowType,
						'allowMime' => $allowMime,
						'web_domain' => $GLOBALS['web_upload'][$_GET['web_type']]['web_domain'],
						'server_path' => DOC_ROOT.$GLOBALS['web_upload'][$_GET['web_type']]['path'],
					);
		
		//验证是否含有
		if ( in_array($_REQUEST['action'], array('del', 'upload') ) ) {
			$file_row = checkIsTask($GLOBALS['web_upload'][$_GET['web_type']]['site_id'], 0);
			unset($file_row['mState']);
			$data = array_merge($data, $file_row);
		}

		$file = new file($data);
		//删除目录和文件
		if ($_REQUEST['action'] == 'del') {
			
			if ($filenames = $_REQUEST['filenames']) {
				if (is_string($filenames)) {
					$file->rm($filenames);
				} elseif (is_array($filenames)) {
					
					foreach($filenames as $filename) {
						$file->rm($filename);
					}
				}
			}
			 
			//删除目录
			
			if ($dirnames = $_REQUEST['dirpath']) {
				
				if (is_string($dirnames)) {
		
					$file->deltree($dirnames);
					
				} elseif (is_array($dirnames)) {
					
					foreach($dirnames as $dirname) {
						
						$file->deltree($dirname);
					}
				}
			}
	
			alert('删除成功');
			exit;
		} else if ($_REQUEST['action'] == 'mkdir') {//创建目录操作
			
			$mdir = $_REQUEST['dir'];
			
			if (eregi("[^0-9a-z_-]",$mdir)) {
				 
					
				alert("目录名含有非法字符，请重试", 'location', $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
		
			}
			
			if ($file->makeDir($mdir)) {
				alert("创建目录成功", 'location', $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
		
			}
		} else if ($_REQUEST['action'] == 'upload') {//上传操作
			

			if ($_FILES['user_file']['name'] != '') {
		
				if ($fname = $file->upload('user_file')) {
					
					if ($_REQUEST['unzip'] == 'true') {
						$file->unZip($fname['file'], $fname['path']);
						$file->rm($fname['file']);
					}
					$message = '上传成功';
				} else {
					$message = '上传失败';
				}
		
				alert($message, 'location', $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
				exit;
			}
		}
		
		$files = $dirs = array();
		
		//得到文件目录以及文件
		$fs = $file->getFiles();
		
		//文件夹列表操作
		if (!empty($fs['dirs'])) {
			sort($fs['dirs']);
			foreach ($fs['dirs'] AS $key => $value) {
				
				$dirs[$key]['path_dirs'] = iconv('gb2312', 'utf-8', $path.'/'.$value);
				$dirs[$key]['dirs'] = iconv('gb2312', 'utf-8', $value);
			}
		}
		
		//文件列表操作
		if (!empty($fs['files'])) {
			foreach ($fs['files'] AS $key => $value) {
				foreach ($value AS $key1 => $value1) {
				
					$file_path = 'http://check.wondershare.com'.$GLOBALS['web_upload'][$_GET['web_type']]['path'].$path.'/'.$value1;
					$files[$key][$key1]['file_path'] = iconv('gb2312', 'utf-8', $file_path);
					$files[$key][$key1]['file_name'] = iconv('gb2312','utf-8',$value1);
					$files[$key][$key1]['file_mtime'] = date("Y-m-d H:i:s", filemtime(iconv('gb2312', 'utf-8', $path.'/'.$value1)));
					$files[$key][$key1]['file_size'] = $file->getRealSize(filesize(iconv('gb2312', 'utf-8', $path.'/'.$value1)));
				}
			}
		}
		
		
		chdir(EXE_ROOT.'/wondershare');
		$GLOBALS['smarty']->assign('web_type',  $_GET['web_type']);
		$GLOBALS['smarty']->assign('site_id', (int) $GLOBALS['web_upload'][$_GET['web_type']]['site_id']);
		
		$GLOBALS['smarty']->assign('dirs',   $dirs);
		$GLOBALS['smarty']->assign('files',   $files);
		$GLOBALS['smarty']->assign('file_sum',   count($files));
		
		$GLOBALS['smarty']->assign('path',   iconv('gb2312','utf-8',$path));
		$GLOBALS['smarty']->assign('file_nav',  $this->formatNav(iconv('gb2312','utf-8', $path) , $_GET['web_type']));
		
		$GLOBALS['smarty']->display('file_index.htm'); 
	
	}
	

	//得到文件目录导航
	function formatNav($path, $web_type) {
		
		$path = ereg_replace("^[\.\\\/]+","",$path);
		$ps = split('[\\\/]',$path);
		$nav = "<a href='?path=./&web_type=".$web_type."'>/</a>";
		
		if (count($ps) > 0) {
			$url = '';
			foreach ($ps as $key => $p) {
				
				if (!empty($p)) {
					
					$url .= $p.'/';
					
					if (ereg_replace("[\.\\\/]","",$url) == ereg_replace("[\.\/\\]","",$path)) {
						$nav .= "$p";
					} else {
						$nav .= "<a href='?path=$url&web_type=".$web_type."'>$p/</a>";
					}
				}
			}
		}
		return $nav;
	}
	


}


$action    = $_REQUEST['action']; 
$upload_file  = new Upload_File;
if (method_exists($upload_file, $action))  {
	
	$upload_file->$action();
} else {
	
	$upload_file->index();
}

?>
